<?php

namespace app\api\controller;

use app\admin\model\cases\Ctype;
use app\api\validate\CasesValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 案件管理
 */
class Cases extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\api\model\Cases();
        $this->modelType = new Ctype();
        $this->modelShop = new \app\admin\model\shop\User();
        $this->modelUser = new \app\admin\model\userstaff\User();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }
    }


    /**
     *  列表
     * @return void
     */
    public function  list()
    {
        $input = input();
        //根据不同的人显示不同的数据
        $userinfo = $this->auth->getUserinfo();
        try {
            //状态:1=巡查人员,2=队长,3=支队长
            switch ($userinfo['type']) {
                case 1:
                    $where['user_staff_id'] = $userinfo['staff_id'];
                    break;
                case 2:
                    $where['captain_id'] = $userinfo['staff_id'];
                    break;
                case 3:
                    $where['detachment_id'] = $userinfo['staff_id'];
                    break;
                default;
                    $where['status'] = 1000;
                    break;
            }

            if (!empty($input['status'])) {
                $where['status'] = $input['status'];
                if ($input['status'] === 2) {
                    $where['status'] = ['in', [2, 3]];
                }
            }

            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->where($where)
                ->order('id desc')
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);

            foreach ($data->items() as &$v) {
                $v['cases_type_name'] = $this->modelType->where('id', $v['cases_type_id'])
                    ->value('name') ?? '其他';
                $v['accept_name'] = $this->modelUser->where('id', $v['accept_id'])
                    ->value('name') ?? '待解签';
                $v['shop_info'] = $this->modelShop->where('id', $v['user_shop_id'])
                    ->field('name,license,coverimage,city,merchant_address')->find();
            }
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     *   解签。 结案
     * @return void
     */
    public function save()
    {
        $input = input();
        try {
            $this->validate($input, CasesValidate::class . '.save');
            $casesStatus = $this->model->where('id', $input['id'])
                ->value('status');
            if (!$casesStatus) {
                throw new Exception('未找到案件');
            }
            $data = [];
            if ($input['type'] == 1) {
                if ($casesStatus != 1) {
                    throw new Exception('当前案件已解签');
                }
                $data = ['status' => 2, 'accept_id' => $this->auth->getUserinfo()['staff_id']];
            }
            if ($input['type'] == 2) {
                if ($casesStatus == 4) {
                    throw new Exception('当前案件已结案');
                }
                if ($casesStatus == 1) {
                    throw new Exception('请先解签');
                }
                $data = ['status' => 4];
            }
            $patrol = $this->model->where('id', $input['id'])
                ->update($data);
            if (!$patrol) {
                throw  new Exception('确认失败');
            }
            $this->success('确认成功', ['id' => $input['id']]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 支队长
     * 案件另行处罚，进入待核实案件
     */
    public function verify()
    {
        $input = input('post.');
        try {
            $this->validate($input, CasesValidate::class . '.verify');

            $status = $this->model->where('id', $input['id'])->value('status');
            if (!$status) {
                throw  new Exception('信息未找到');
            }
            if ($status == 4) {
                throw  new Exception('该案件已结案无法重复提交');
            }
            if ($status == 3) {
                throw new Exception('请勿重复提交');
            }
            if ($status == 1) {
                throw new Exception('请先解签案件');
            }
            $this->model->where('id', $input['id'])->update([
                'verify_content' => $input['verify_content'],
                'verify_images' => $input['verify_images'],
                'status' => 3,
            ]);
            $this->success('提交成功', ['id' => $input['id']]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 巡查处罚
     * 案件另行处罚后直接结案
     */
    public function punish()
    {
        $input = input('post.');
        try {
            $this->validate($input, CasesValidate::class . '.punish');

            $status = $this->model->where('id', $input['id'])->value('status');
            if (!$status) {
                throw  new Exception('信息未找到');
            }
            if ($status != 3) {
                throw new Exception('请勿重复提交');
            }
            $this->model->where('id', $input['id'])->update([
                'verify_money' => $input['verify_money'],
                'status' => 4,
            ]);

            $this->success('提交成功', ['id' => $input['id']]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }


    /**
     * 案件上报
     * @return void
     */
    public function appear()
    {
        $input = input();
        try {
            $this->validate($input, CasesValidate::class . '.appear');
            $casesStatus = $this->model->where('id', $input['id'])
                ->value('is_appear');
            if (!$casesStatus) {
                throw new Exception('未找到案件');
            }
            if ($casesStatus != 1) {
                throw new Exception('请勿重发上报');
            }
            $this->model->where('id', $input['id'])
                ->update(['is_appear' => 2]);
            $this->success('上报成功', ['id' => $input['id']]);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }
}



